<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "pet".
 *
 * @property int $pid
 * @property string $pet_hash 宠物哈希
 * @property int $style 分别对应不同的图片
 * @property int $is_auction 0、普通宠物             1、拍卖宠物
 * @property int $algebra 代数
 * @property string $height 体重
 * @property string $nuggets 掘金
 * @property string $grow 成长
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $birth_type 1、系统抽奖产生 2、生育产生
 * @property int $sale_status 0、未挂卖             1、挂卖中             2、拍卖中
 * @property int $updated_at 修改时间
 * @property string $price 拍卖价格
 * @property int $birth_nuggets 掘金
 * @property int $birth_grow 成长
 */
class Pet extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pet';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['height', 'nuggets', 'grow', 'price','birth_nuggets','birth_grow'], 'number'],
            [['created_at', 'updated_at'], 'integer'],
            [['pet_hash'], 'string', 'max' => 64],
            [['style', 'is_auction', 'algebra', 'status', 'birth_type', 'sale_status'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pid' => 'Pid',
            'pet_hash' => '宠物哈希',
            'style' => '分别对应不同的图片',
            'is_auction' => '0、普通宠物
            1、拍卖宠物',
            'algebra' => '代数',
            'height' => '体重',
            'nuggets' => '掘金',
            'grow' => '成长',
            'status' => '状态',
            'created_at' => '创建时间',
            'birth_type' => '1、系统抽奖产生 2、生育产生',
            'sale_status' => '0、未挂卖1、挂卖中2、拍卖中',
            'updated_at' => '修改时间',
            'price' => '拍卖价格',
            'birth_grow' => '出生时的体重',
            'birth_nuggets' => '出生时的掘金',
        ];
    }

    /**
     * @param $userid
     * @param $date
     */
    public function setPetHash($userid, $date)
    {
        $this->pet_hash = hash('ripemd160', $userid.$date);
    }

    /**
     * 生成拍卖宠物
     * @author lizhi7  <lizhi7@xin.com>
     * @param $nuggets
     * @param $grow
     * @return string
     */
    public function addAuctionPet($nuggets, $grow)
    {
        $date = date('Y-m-d H:i:s');
        $userId = 0;
        $this->pet_hash = hash('ripemd160', $userId.$date);
        $this->style = rand(1,12);
        $this->is_auction = 1;
        $this->height = 0;
        $this->algebra = 0;
        $this->nuggets = $nuggets;
        $this->grow = $grow;
        $this->birth_nuggets = $nuggets;
        $this->birth_grow = $grow;
        $this->status = 0;
        $this->created_at = time();
        $this->birth_type = 3;
        $this->sale_status = 0;
        $this->updated_at = time();
        $this->price = 0;
        $this->save();
        $this->validate();
        return \Yii::$app->db->getLastInsertId();
    }

    /**
     * 增加默认的玩客猪
     * @author lizhi7  <lizhi7@xin.com>
     * @return string
     */
    public function addDefaultPet()
    {
        $userId = Yii::$app->user->getId();
        $date = time();
        $this->setPetHash($userId, $date);
        $this->style = rand(1,12);
        $this->is_auction = 0;
        $this->height = 0;
        $this->algebra = $this->getAlgebra();
        $this->nuggets = rand(10, 20)/10;
        $this->grow = rand(10, 20)/10;
        $this->birth_nuggets = $this->nuggets;
        $this->birth_grow = $this->grow;
        $this->status = 0;
        $this->created_at = time();
        $this->birth_type = 1;
        $this->sale_status = 0;
        $this->updated_at = time();
        $this->price = 0;
        $this->save();
        $this->validate();
        return \Yii::$app->db->getLastInsertId();
    }

    /**
     * 抽奖根据hash匹配玩客猪
     * @author lizhi7  <lizhi7@xin.com>
     * @return int
     */
    public function getAlgebra()
    {
        $patterns = "/\d+/"; //第一种;
        preg_match_all($patterns, $this->pet_hash,$arr);
        $result_keyed = array_reverse($arr[0], true);
        $str = implode('', $result_keyed);
        $a1 = substr($str, 0,1);
        $a2 = substr($str, 1,1);
        $a3 = substr($str, 2,1);
        $a4 = substr($str, 3,1);
        $a5 = substr($str, 4,1);
        if ($a1 == $a2 && $a2 == $a3 && $a3 == $a4 && $a4 == $a5) {
            return 1;
        }
        if ( ($a1 == $a2 && $a2 == $a3 && $a3 == $a4) || ($a2 == $a3 && $a3 == $a4 && $a4 == $a5)) {
            return 2;
        }

        if ( ($a1 == $a2 && $a2 == $a3 && $a3 == $a4)) {
            return 3;
        }

        if ( ($a1 == $a2 && $a2 == $a3) || ($a2 == $a3 && $a3 == $a4) || ($a3 == $a4 && $a4 == $a5)) {
            return 4;
        }

        if ( ($a1 == $a2 && $a2 == $a3) || ($a2 == $a3 && $a4)) {
            return 5;
        }
        return 6;
    }

    public function getPetBack()
    {
        $back = 0;
        if(in_array($this->style, [1,2])) {
            $back = 1;
        } else if (in_array($this->style, [2,3])) {
            $back = 3;
        } else if (in_array($this->style, [4,5])) {
            $back = 4;
        } else if (in_array($this->style, [5,6])) {
            $back = 5;
        } else if (in_array($this->style, [7,8])) {
            $back = 6;
        } else if (in_array($this->style, [9,10])) {
            $back = 7;
        } else if (in_array($this->style, [11,12])) {
            $back = 8;
        }
        return $back;
    }

    /**
     * @return $this
     */
    public function getAuctionList()
    {
        return $this->find()->where('is_auction=1 and sale_status=2')
            ->with('UserPet')
            ->with('Auction')
            ->orderBy(['pid' => SORT_DESC])->all();
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserPet()
    {
        return $this->hasOne(UserPet::className(), ['pid' => 'pid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAuction()
    {
        return $this->hasOne(Auction::className(), ['pid' => 'pid']);
    }

}
